from django.shortcuts import render
from .models import *
from .serializers import *
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.generics import GenericAPIView
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
from rest_framework.viewsets import ModelViewSet
from rest_framework.mixins import *
# Create your views here.


# 部门视图
class DepartmentAPIView(ListCreateAPIView):
    queryset = Department.objects.all()
    serializer_class = DepartmentModelSerializer

    # 获取所有的部门数据
    # def get(self, request, *args, **kwargs):
        # deps = self.get_queryset()
        # deps_ser = self.get_serializer(deps, many=True)
        #
        # return Response(deps_ser.data)
        # return self.list(request, *args, **kwargs)

    # def post(self, request, *args, **kwargs):
        # 添加一个部门
        # name = request.data.get("name")
        # dep = Department.objects.filter(name=name)
        # if dep:
        #     return Response({"code": 400, "msg": "部门已存在"})

        # 继续添加, 实例化一个序列化器对象
        # ser = self.get_serializer(data=request.data)
        #
        # # 数据校验
        # if ser.is_valid():
        #
        #     # 校验通过 保存数据     create
        #     ser.save()
        #     return Response({"code": 200, "msg": "添加部门成功！"})
        #
        # else:
        #     return Response({"error": ser.errors, "code": 400})
        # return self.create(request, *args, **kwargs)


# 部门详情的视图
class DepartmentInfoAPIView(RetrieveUpdateDestroyAPIView):
    queryset = Department.objects.all()
    serializer_class = DepartmentModelSerializer
    lookup_field = "pk"
    lookup_url_kwarg = "pk"

    # 查询一个部门
    # def get(self, request, pk):
        # try:
        #     dep = self.get_object()
        #
        #     dep_ser = self.get_serializer(dep)
        #
        #     return Response(dep_ser.data)
        # except:
        #     return Response({"code": 400, "msg": "部门不存在"})
        # return self.retrieve(request, pk)

    # 更新一个部门
    # def put(self, request, pk):
        # try:
        #     dep = self.get_object()
        #     # 中间的判断逻辑省略
        #     ser = self.get_serializer(instance=dep, data=request.data)
        #
        #     # 数据校验
        #     ser.is_valid(raise_exception=True)
        #     # 保存数据
        #     ser.save()   # 调用的update方法
        #
        #     return Response({"code": 200, "msg": "更新成功"})
        # except:
        #     return Response({"code": 400, "msg": "部门不存在"})
        # return self.update(request, pk)

    # 删除一个部门
    # def delete(self, request, pk):

        # dep = Department.objects.filter(id=pk).first()
        #
        # dep.delete()
        #
        # return Response({"code": 200, "msg": "删除成功！"})
        # return self.destroy(request, pk)


# 定义 员工的 视图集
class StaffViewSet(ModelViewSet):
    queryset = Staff.objects.all()
    serializer_class = StaffModelSerializer
    lookup_field = "pk"
    lookup_url_kwarg = "pk"

    # def list(self, request, *args, **kwargs):
    #     ...
    #
    # def create(self, request, *args, **kwargs):
    #     ...
    #
    # def retrieve(self, request, *args, **kwargs):
    #     ...
    # def update(self, request, *args, **kwargs):
    #     ...
    # def destroy(self, request, *args, **kwargs):
    #     ...

    


